﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Data;
using System.IO;

public partial class Notes_NTS_AddNotes : System.Web.UI.Page
{ 
    Notes.NotesDB _objNotesDB = new Notes.NotesDB();
    RegistrationData _objRegistrationData = new RegistrationData();  


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!object.Equals(Session["User"], null))
            {
                BindStream();
                BindUniversity();
            }
            else
            {
                Response.Redirect(Common.NotesLogOutPage);
            }
        }
    }

    protected void BindStream()
    {
        IDataReader dr;
        ddlStream.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlStream.Items.Add(lst1);
        dr = _objNotesDB.Select_Streams();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["Stream"].ToString();
                lst.Value = dr["StreamID"].ToString();
                ddlStream.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void BindUniversity()
    {
        IDataReader dr;
        ddlUniversity.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlUniversity.Items.Add(lst1);
        dr = _objNotesDB.Select_Universities();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["UniversityName"].ToString();
                lst.Value = dr["UniversityID"].ToString();
                ddlUniversity.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void BindSubject()
    {
        int StreamID = 0;
        IDataReader dr;
        ddlSubject.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlSubject.Items.Add(lst1);
        if (ddlStream.SelectedItem.Text != "Select")
        {
            StreamID = Convert.ToInt32(ddlStream.SelectedValue.ToString());
        }
        else
        {
            StreamID = 0;
        }
        dr =_objNotesDB.Select_Subjects(StreamID);
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["SubjectName"].ToString();
                lst.Value = dr["SubjectID"].ToString();
                ddlSubject.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = (RegistrationData)Session["User"]; 
        }
        if (ValidatePage())
        {
            try
            {
                 
                if (ddlStream.SelectedItem.Text != "Select")
                   _objNotesDB.StreamID = Convert.ToInt16(ddlStream.SelectedValue.ToString());
                else
                    _objNotesDB.StreamID = 0;

                if (ddlSubject.SelectedValue!= "0")
                    _objNotesDB.SubjectID = Convert.ToInt16(ddlSubject.SelectedValue);
                else
                    _objNotesDB.SubjectID = 0;

             
                _objNotesDB.Title = txtTitle.Text;
                _objNotesDB.Description = txtDescription.Text;
                _objNotesDB.UniversityID = Convert.ToInt16(ddlUniversity.SelectedValue);
                _objNotesDB.Document = FileUpload1.PostedFile;
                _objNotesDB.DocTypeID = Convert.ToInt16(ddlDocType.SelectedValue);

                int strResult = 0;
                strResult = _objNotesDB.Insert_Document();
                if (strResult > 0)
                {

                    lblMsg.Text = "Your document posted successfully, to view your document <a href='/Notes/NTS-DocDetails.aspx?DocID=" + strResult.ToString() + "' style='color:#01a489'>click here.</a>";
                    reset();
                }
                else
                {
                    lblMsg.Text = "Some error occured please try after some time";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
    }


    void reset()
    {
        txtTitle.Text = string.Empty;
        ddlUniversity.SelectedIndex = 0;
        txtDescription.Text = string.Empty;
        ddlStream.SelectedIndex = 0;
        ddlSubject.SelectedIndex = 0; 
    }

    #region for Validation

    protected bool ValidatePage()
    {
        bool IsValidated = true;
        if (MandatoryFieldsCheck())
        {
            if (!PatternCheck())
            {
                IsValidated = false;
            }
        }
        else
        {
            IsValidated = false;
        }
        return IsValidated;
    }

    protected bool MandatoryFieldsCheck()
    {
        if (txtTitle.Text == string.Empty)
        {
            lblMsg.Text = "Required field - Title";
            return false;
        }
        if (ddlStream.SelectedItem.Text == "Select")
        {
            lblMsg.Text = "Required field - Stream";
            return false;
        }

        if (ddlSubject.SelectedItem.Text == "Select")
        {
            lblMsg.Text = "Required field - Subject";
            return false;
        }
        if (ddlDocType.SelectedItem.Text == "Select")
        {
            lblMsg.Text = "Required field - Document Type";
            return false;
        }
        if (FileUpload1.PostedFile == null && FileUpload1.FileName == string.Empty)
        {
            lblMsg.Text = "Required field - File";
            return false;
        } 

        return true;
    }

    protected bool PatternCheck()
    {
        return true;
    }

    #endregion

    protected void ddlStream_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubject();
    }
      
     
 
}